package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import model.ComplaintReportModel;
import view.ComplaintReportGUI;
import client.CliMessage;

/**
 * The Class ComplaintReportController that extends AbstractController implements IValidateInput.
 * @author yudku
 */
public class ComplaintReportController extends AbstractController implements IValidateInput
{	
	
	/** The Complaint report view. */
	private ComplaintReportGUI ComplaintReportView;
	
	/** The Comp report model. */
	private ComplaintReportModel CompReportModel;
	
	/**
	 * Instantiates a new complaint report controller.
	 *
	 * @param ComplaintReportView the complaint report view
	 * @param CompReportModel the comp report model
	 */
	public ComplaintReportController( ComplaintReportGUI ComplaintReportView, ComplaintReportModel CompReportModel) 
	{
		super(ComplaintReportView,CompReportModel);
		this.ComplaintReportView = ComplaintReportView;
		this.CompReportModel = CompReportModel;
	}

	/**
	 * Sets the buttons.
	 *
	 * @param CompReportModel the new buttons
	 */
	public void setButtons(ComplaintReportModel CompReportModel)
	{		
		ComplaintReportView.addReplyActionListener(new ReplyListener(CompReportModel));
	}

	@Override
	public boolean validateInput() 
	{
		return true;		
	}

	/**
	 * Inner class that handles when Reply Button Pressed, implements Action listener.
	 *
	 * @author Tal & Yuda
	 */
	
	class ReplyListener implements ActionListener
	{
		
		/** The Comp report model. */
		private ComplaintReportModel CompReportModel;
		
		/**
		 * Instantiates a new reply listener.
		 *
		 * @param CompReportModel the comp report model
		 */
		public ReplyListener(ComplaintReportModel CompReportModel)
		{
			this.CompReportModel = CompReportModel;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			if(!validateInput())
				return;
	  
			int comNum = CompReportModel.getcomplaintNum();
			int unNum = CompReportModel.getunhandledComp();
			int monthNum =  CompReportModel.getmonthleycomp();
       
			//insert reply to complaint id and change status in db
           
			String insertQuery = "adu"	+"INSERT INTO complaint_report "
										+"(complaintNum ,unhandledComplaints, monthlyHandled) VALUES "
										+"("+ comNum +","+ unNum +","+ monthNum +")";

			
             //activate the back button
		  	   ComplaintReportView.clickBackButton();
               sendToServer(insertQuery); 
		}
	}

	@Override
	public void handleDBRequestSuccess()
	{
		ComplaintReportView.displayWrnMessage("Reply Succesfully Enterd");		
	}

	@Override
	public void handleDBSearchResult(CliMessage dbResult)
	{	
		int unhandledNum = 0;
		int monthleyNum = 0;

		ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
		
		Calendar cal = Calendar.getInstance();	
		int currMonth = cal.get(Calendar.MONTH);
		currMonth++;
		int currYear = cal.get(Calendar.YEAR);
		int complaintNum=srs.size();
		for(int i = 0 ; i < complaintNum  ; i++)
		{
		   Map<String, String> complaintTupple  = srs.get(i);	
		   if ( complaintTupple.get("STATUS").equals("0"))
			   unhandledNum++;

		   
		   int DBmonth = (Integer.parseInt((complaintTupple.get("DATE")).substring(5, 7)));
		   int DByear = (Integer.parseInt((complaintTupple.get("DATE")).substring(0, 4)));
		   int DBday = (Integer.parseInt((complaintTupple.get("DATE")).substring(8, 10)));
		   
		   System.out.println ("year: " + DByear + " month: " + DBmonth + " day: " + DBday);
					
		   if ( !(complaintTupple.get("STATUS").equals("0")) && (currMonth == DBmonth) && (currYear == DByear) && ( DBday >= 1) && ( DBday <=31))
			   monthleyNum++;


		   CompReportModel.setcomplaintNum(complaintNum);
		   CompReportModel.setunhandledComp(unhandledNum);
		   CompReportModel.setmonthleycomp(monthleyNum);
		}	

		
		///set fields GUi
		ComplaintReportView.SetGenLabel(Integer.toString(CompReportModel.getcomplaintNum()),  ComplaintReportView.getlabel1_2());
		ComplaintReportView.SetGenLabel(Integer.toString(CompReportModel.getunhandledComp()),  ComplaintReportView.getlabel2_2());
		ComplaintReportView.SetGenLabel( Integer.toString(CompReportModel.getmonthleycomp()),  ComplaintReportView.getlabel3_2());

		
		setButtons(CompReportModel);
		//display main window
		ComplaintReportView.setVisible(true);						
	}

	@Override
	public void handleNullSearchResult() 
	{
		ComplaintReportView.displayWrnMessage("DATA DOES NOT EXIST!");
		
	}

}
